package com.lb.jwitter.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import com.lb.jwitter.service.TblCTDTDMNgayTrongTuan;
import com.lb.jwitter.service.TblCTDTDMNgayTrongTuanService;
import com.lb.jwitter.service.TblCTDTDMPhong;
import com.lb.jwitter.service.TblCTDTDMPhongService;
import com.lb.jwitter.service.TblCTDTDMTietHoc;
import com.lb.jwitter.service.TblCTDTDMTietHocService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.service.TblThongTinTKBChiTiet;
import com.lb.jwitter.service.TblThongTinTKBChiTietService;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class TblThongTinNhomLopMonHoctblThongTinTKBChiTietEditFormAction {
	private int id; 
    public void setId(int id) {
       this.id = id;
 }

    public int getId() {
       return id;
 }
    private ArrayList<String> tuanHocs;

	public ArrayList<String> getTuanHocs() {
		return tuanHocs;
	}

	public void setTuanHocs(ArrayList<String> tuanHocs) {
		this.tuanHocs = tuanHocs;
	}

	private Collection<TblCTDTDMPhong> tblCTDTDMPhongs; 
    public Collection<TblCTDTDMPhong>  getTblCTDTDMPhongs(){ 
              return tblCTDTDMPhongs; 
     } 

    private TblCTDTDMPhongService tblCTDTDMPhongService; 

    private Collection<TblCTDTDMNgayTrongTuan> tblCTDTDMNgayTrongTuans; 
    public Collection<TblCTDTDMNgayTrongTuan>  getTblCTDTDMNgayTrongTuans(){ 
              return tblCTDTDMNgayTrongTuans; 
     } 

    private TblCTDTDMNgayTrongTuanService tblCTDTDMNgayTrongTuanService; 

    private Collection<TblCTDTDMTietHoc> tblCTDTDMTietHocs; 
    public Collection<TblCTDTDMTietHoc>  getTblCTDTDMTietHocs(){ 
              return tblCTDTDMTietHocs; 
     } 

    private TblCTDTDMTietHocService tblCTDTDMTietHocService; 
    
    private TblThongTinTKBChiTietService tblThongTinTKBChiTietService;
	
    private TblThongTinTKBChiTiet tblThongTinTKBChiTiet;
    
    public TblThongTinTKBChiTiet getTblThongTinTKBChiTiet() {
		return tblThongTinTKBChiTiet;
	}

	public void setTblThongTinTKBChiTiet(TblThongTinTKBChiTiet tblThongTinTKBChiTiet) {
		this.tblThongTinTKBChiTiet = tblThongTinTKBChiTiet;
	}
	private TblTaiKhoan taiKhoanDangNhap;

	public TblTaiKhoan getTaiKhoanDangNhap() {
		return taiKhoanDangNhap;
	}

	public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) {
		this.taiKhoanDangNhap = taiKhoanDangNhap;
	}

	private TblQuyenChiTietService tblQuyenChiTietService;
	@Autowired
	// Injected by Spring
	ApplicationContextProvider contextProvider;

	public String execute() throws Exception {
		try {
			Map session = ActionContext.getContext().getSession();
			taiKhoanDangNhap = (TblTaiKhoan) session.get("taiKhoan");
			if (taiKhoanDangNhap == null)
				return "noprivilege";
			ApplicationContext context = ApplicationContextProvider
					.getApplicationContext();
			tblQuyenChiTietService = (TblQuyenChiTietService) context
					.getBean("tbl_Quyen_ChiTietClient");
			if (!tblQuyenChiTietService.checkRight(taiKhoanDangNhap
					.getMaNhomQuyen().getId(), "tbl_ThongTin_TKB_ChiTiet",
					"xem")) {
				return "noprivilege";
			}
			
	        tblThongTinTKBChiTietService = (TblThongTinTKBChiTietService) context.getBean("tbl_ThongTin_TKB_ChiTietClient"); 

	        tblCTDTDMPhongService = (TblCTDTDMPhongService) context.getBean("tbl_CTDT_DM_PhongClient"); 

	        tblCTDTDMNgayTrongTuanService = (TblCTDTDMNgayTrongTuanService) context.getBean("tbl_CTDT_DM_NgayTrongTuanClient"); 

	        tblCTDTDMTietHocService = (TblCTDTDMTietHocService) context.getBean("tbl_CTDT_DM_TietHocClient");
	        
	        tblCTDTDMNgayTrongTuans =       tblCTDTDMNgayTrongTuanService.findAlltblCTDTDMNgayTrongTuan(0, 0); 
            tblCTDTDMTietHocs =       tblCTDTDMTietHocService.findAlltblCTDTDMTietHoc(0, 0); 
            tblCTDTDMPhongs = tblCTDTDMPhongService.findAlltblCTDTDMPhong(0, 0);
            tblThongTinTKBChiTiet = tblThongTinTKBChiTietService.gettblThongTinTKBChiTiet(new Long(id));
            
            tuanHocs = new ArrayList<String>();
            if(tblThongTinTKBChiTiet.getTuanHoc() != null){
    	        //tuanHocs = Arrays.asList(Integer.toBinaryString(tblThongTinTKB.getTuanHoc()).split("(?!^)"));
            	String s = Integer.toBinaryString(tblThongTinTKBChiTiet.getTuanHoc());
            	tuanHocs = new ArrayList<String>(Arrays.asList(s.split("(?!^)")));
            }
            if(tuanHocs.size() < 26){
            	for(int i = tuanHocs.size(); i < 26; i++){
            		tuanHocs.add(0, "0");
            	}
            }
	        
	        return Action.SUCCESS; 
		} catch (Exception e) {
			System.out
					.println("Loi EditForm tbl_ThongTin_TKB_ChiTiet Action");
			e.printStackTrace();
			return Action.ERROR;
		}
	}
}
